package markgame2dphys;

import java.awt.Color;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JFrame;

import net.phys2d.math.Vector2f;
import net.phys2d.raw.Body;

public class Test1_Objetos extends MarkGame2DEngine{

	private static final long serialVersionUID = 1L;

	public static final int W = 640;
	public static final int H = 480;

	public MarkGameBox box1;
	public MarkGameCircle circle1;
	public MarkGamePolygon poly1;
	public MarkGameBox ground1;

	public MarkGameBox boxLeft;
	public MarkGameBox boxRight;
	public MarkGameJoint joint1;

	public enum JointType{BASIC,DISTANCE,FIXED,SLIDE,SPRING};
	
	public JointType jointType = JointType.SLIDE;

	public Test1_Objetos() {
		super(W,H,0,100,10, 20);

		box1 = new MarkGameBox(50, 50, 1);
		box1.body.setPosition(W/2+40, 100);
		add(box1);

		circle1 = new MarkGameCircle(50, Body.INFINITE_MASS);
		circle1.body.setPosition(W/2, H-100);
		add(circle1);

		List<Vector2f> points3 = new LinkedList<Vector2f>();
		for (int a = 0, i = 0; a < 360; a+=5, i = 1-i) {
			double radius = 30 + i*5;
			double angle = Math.toRadians(a);
			double x = Math.cos(angle)*radius;
			double y = Math.sin(angle)*radius;
			points3.add(new Vector2f((float)x-15, (float)y+0));
		}
		poly1 = new MarkGamePolygon(points3.toArray(new Vector2f[]{}), 1);
		poly1.body.setPosition(W/2-30, 100);
		add(poly1);

		ground1 = new MarkGameBox(W-10, 10, Body.INFINITE_MASS);
		ground1.body.setPosition(W/2, H-10);
		add(ground1);

		boxLeft = new MarkGameBox(50, 10, Body.INFINITE_MASS);
		boxRight = new MarkGameBox(50, 10, 1);
		boxLeft.body.setPosition(W/2-25+20, 10);
		boxRight.body.setPosition(W/2+25+20, 10);

		boxLeft.body.addExcludedBody(boxRight.body);
		boxRight.body.addExcludedBody(boxLeft.body);
		add(boxLeft);
		add(boxRight);

		switch (jointType) {
		case BASIC:
			joint1 = new MarkGameBasicJoint(
					boxLeft,
					boxRight,
					new Vector2f(W/2+20, 0));
			break;
		case DISTANCE:
			joint1 = new MarkGameDistanceJoint(
					boxLeft,
					boxRight,
					new Vector2f(23,0),
					new Vector2f(-23,0),
					0f);
			break;
		case FIXED:
			joint1 = new MarkGameFixedJoint(boxLeft, boxRight);
			break;
		case SLIDE:
			joint1 = new MarkGameSlideJoint(
					boxLeft,
					boxRight,
					new Vector2f(23,0),
					new Vector2f(-23,0),
					10, 100, 0.5f);
			break;
		case SPRING:
			boxRight.body.setPosition(W/2+200, 200);
			joint1 = new MarkGameSpringJoint(
					boxLeft,
					boxRight,
					new Vector2f(boxLeft.body.getPosition().getX()+23,boxLeft.body.getPosition().getY()),
					new Vector2f(boxRight.body.getPosition().getX()-23,boxRight.body.getPosition().getY()),
					200, 100, 300, 0.5f);
			break;
		}
		
		add(joint1);
	}

	public void paint(MarkGameGraphics g) {

		g.setColor(Color.WHITE);
		g.fillRect(0,0,W,H);

		box1.paint(g);

		circle1.paint(g);

		poly1.paint(g);

		ground1.paint(g);

		boxLeft.paint(g);
		boxRight.paint(g);
		
		joint1.paint(g);
	}

	

	public static void main(String[] args) {

		JFrame janela = new JFrame("teste1");
		janela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		Test1_Objetos app = new Test1_Objetos();

		janela.getContentPane().add(app);

		janela.pack();
		janela.setVisible(true);

		janela.setLocationRelativeTo(null);

		janela.setExtendedState(JFrame.MAXIMIZED_BOTH);

		new Thread(app).start();

	}

}
